<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\controller\driver;
use think\Config;
class Base extends \app\api\controller\Base
{
    public $driverInfo;
    public $redisPreFix = 'driver.';

    /**
     * 更新司机缓存位置
     * @param $lon
     * @param $lat
     * @param $report_time
     * @param $arr
     */
    public function updateRedisDriverPos($lon,$lat,$report_time,$arr=[]){
        $info             = unserialize($this->redis->hget($this->key,$this->token));
        if (!empty($info['gps_time']) && $report_time <= $info['gps_time']) return;
        $info['gps_time'] = $report_time;
        $info['lon']      = $lon;
        $info['lat']      = $lat;
        foreach($arr as $k=>$v){
            $info[$k] = $v;
        }
        $posStr       = serialize($info);
        $this->redis->lpush('car.track.4',$posStr);
        $this->redis->hset($this->key,$this->token,$posStr);
    }

    /**
     * 批量参数获取
     * @param $string
     * @return array
     */
    public function getMassParam($string){
        $ret = [];
        if(empty($string)){
            return [];
        }
        $arr = explode(',',$string);
        foreach ($arr as $v){
            $arr2 = explode('|', $v);
            if(count($arr2)>1){
                $ret[] = input($arr2[0],$arr2[1]);
            }else{
                $ret[] = input($v);
            }
        }
        return $ret;
    }

    /**
     * 图片转base64上传
     * @param $base64_image_content
     * @param string $dir 路径
     * @return array 上传结果
     */
    public function saveBase64($base64_image_content,$dir='')
    {
        if(empty($base64_image_content)){
            return [0,114];
        }
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
            if(!isset($result[2]) || empty($result[2])){
                return [0,118];
            }
            $type = $result[2] == 'jpeg' ? '.jpg' : '.'.$result[2];
            $fold = "upload/driver/img/".$dir;
            if (!file_exists($fold)) {
                mkdir($fold, 0700);
            }
            $new_file = $fold . date('Ymd', time()) . "/";
            if (!file_exists($new_file)) {
                mkdir($new_file, 0700);
            }
            $new_file = $new_file . md5(uniqid()) . $type;
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
                return [1, $new_file];
            } else {
                return [0, 115];
            }
        }
    }

    /**
     * upd转发定位GPS数据 swoole处理
     */
    public function udpGet($sendMsg = '')
    {
        $ip     = \think\Config::get('udp_server');
        $port   = \think\Config::get('udp_port');
        $handle = stream_socket_client("udp://{$ip}:{$port}", $errno, $errstr);
        if (!$handle) {
            return [0,$errstr];
        }
        fwrite($handle, $sendMsg . "\n");
        $result = fread($handle, 1024);
        fclose($handle);
        return [1,'ok'];
    }

    /**
     * 开启鉴权
     */
    protected $beforeActionList = [
        'auth'
    ];

}
